Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2: Update query keys for partial matching #441

Merged

Conversation

timostamm
Copy link
Member

@timostamm timostamm commented Oct 7, 2024

This changes the type for Connect query keys to be:

type ConnectQueryKey = [
  /**
   * To distinguish Connect query keys from other query keys, they always start with the string "connect-query".
   */
  "connect-query",
  {
    /**
     * A key for a Transport reference, created with createTransportKey().
     */
    transport?: string;
    /**
     * The name of the service, e.g. connectrpc.eliza.v1.ElizaService
     */
    serviceName: string;
    /**
     * The name of the method, e.g. Say.
     */
    methodName?: string;
    /**
     * A key for the request message, created with createMessageKey(),
     * or "skipped".
     */
    input?: Record<string, unknown> | "skipped";
    /**
     * Whether this is an infinite query, or a regular one.
     */
    cardinality?: "infinite" | "finite";
  },
];

The function createConnectQueryKey is updated to accept key parameters as an options object:

createConnectQueryKey({
  transport: myTransport,
  schema: ElizaService.method.say,
  input: create(SayRequestSchema, { sentence: "hi" }),
});

The transport is now part of the key to support multiple transport without tainting the cache (#418).

All key parameters except schema are optional, which allows to create partial keys (#375) for filtering.

The updated function replaces createConnectInfiniteQueryKey. To create a key for an infinite query, use cardinality: "infinite" and the pageParamKey option.

@timostamm timostamm force-pushed the tstamm/v2/Use-message-and-transport-keys-for-query-keys branch from 01934ec to 519e4ad Compare October 7, 2024 10:53
Signed-off-by: Timo Stamm <ts@timostamm.de>
@timostamm timostamm force-pushed the tstamm/v2/Use-message-and-transport-keys-for-query-keys branch from 519e4ad to d4b1fd2 Compare October 7, 2024 10:54
Signed-off-by: Timo Stamm <ts@timostamm.de>
Signed-off-by: Timo Stamm <ts@timostamm.de>
Signed-off-by: Timo Stamm <ts@timostamm.de>
Signed-off-by: Timo Stamm <ts@timostamm.de>
@timostamm timostamm changed the title Use message and transport keys for query keys Update query keys for partial matching Oct 7, 2024
@timostamm timostamm marked this pull request as ready for review October 7, 2024 13:55
Comment on lines +100 to +103
function messageKey(
message: ReflectMessage,
pageParamKey?: string,
): Record<string, unknown> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed that the "page param" is not typically part of the key for infinite queries. Seems like a good fit to support it here. It means that the users doesn't have to omit the page param field with createConnectQueryKey.

Comment on lines +107 to +110
const customTransport = mockPaginatedTransport({
items: ["Intercepted!"],
page: 0n,
});
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The expression needs to move up. Otherwise, a new transport is created every time the hook is rendered, and the key changes.

As noted in #418 (comment), this can be a footgun. I think it should be sufficient to call it out in the documentation, and to give an example. WDYT, @paul-sachs?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Once we've taken transport into account as a key, it's important that it gets memoized

Copy link
Collaborator

@paul-sachs paul-sachs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice cleanup!

): ConnectInfiniteQueryKey {
return [...createConnectQueryKey(schema, input), "infinite"];
const props: ConnectQueryKey[1] =
params.schema.kind == "rpc"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, interesting. I guess this allows for invalidating a whole service if necessary.

@timostamm timostamm changed the title Update query keys for partial matching V2: Update query keys for partial matching Oct 8, 2024
@timostamm timostamm merged commit 92e2997 into v2 Oct 8, 2024
9 checks passed
@timostamm timostamm deleted the tstamm/v2/Use-message-and-transport-keys-for-query-keys branch October 8, 2024 07:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants